<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .container {
      width: 400px;
      margin: 50px auto 0;
    }
    .search-box {
      margin-bottom: 20px
    }
    .suggest-list {
      margin-bottom: 20px
    }
    .suggest-li {
      line-height: 48px;
      border-top: 1px solid #ddd;
    }
    .suggest-li:first-child {
      border-top: none;
    }
  </style>
</head>
<body>
  <div class="container">
    <div class="search-box">输入框: <input class="search-input" /></div>
    <div class="suggest-list">
      <div class="suggest-li">啊</div>
      <div class="suggest-li">啊啊啊</div>
      <div class="suggest-li">啊啊啊啊啊啊</div>
      <div class="suggest-li">啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊</div>
    </div>
  </div>
</body>
<script>
  let isEntering = false
  let result=[]
  let timer;
  const elmInput = document.querySelector('.search-input')
  const elmList = document.querySelector('.suggest-list')
  const ajaxSuggest = (keyword) => {
    if (timer) {
      clearTimeout(timer)
    }
    timer = setTimeout(() => {
      let arr = []
      let random = Math.round(Math.random()*10)
      for(var i=0;i<random;i++) {
        keyword +=keyword.repeat(i+1)
        arr.push(keyword)
      }
      result = arr
      // 渲染
      let fragement = document.createDocumentFragment()
      let str = ''
      for(const txt of result) {
        var li = document.createElement('div')
        li.className="suggest-li"
        li.textContent = txt
        fragement.appendChild(li)
      }
      elmList.innerHTML = ''
      elmList.appendChild(fragement)
    }, 400)
  }
  elmInput.addEventListener('compositionStart', function (e){
    isEntering = true
  })
  elmInput.addEventListener('compositionEnd', function (){
    isEntering = false
    ajaxSuggest(elmInput.value)
  })
  elmInput.addEventListener('change', function (){
    if (isEntering) {
      return;
    }
    ajaxSuggest(elmInput.value)
  })
</script>
</html>